package com.example.pptcontrol;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.Header;
import org.apache.http.HttpResponse;

import org.apache.http.HttpVersion;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.util.EntityUtils;



public class HttpManager {

    private DefaultHttpClient mClient;

    HttpManager() {
        mClient = getHttpClient();
    }


    public com.example.pptcontrol.HttpResponse post(String url, List<HttpParam> additionalHeaderFields, byte[] body) {

        try {
            HttpPost request = new HttpPost(url);
            if (additionalHeaderFields != null) {
                for (HttpParam param : additionalHeaderFields) {
                    request.addHeader(param.getName(), param.getValue());
                }
            }
            request.setEntity(new StringEntity(new String(body, "utf-8")));
            
            org.apache.http.HttpResponse response = mClient.execute(request);
            int status = response.getStatusLine().getStatusCode();

            Map<String, String> headerMap = new HashMap<String, String>();
            for (Header header : response.getAllHeaders()) {
                headerMap.put(header.getName(), header.getValue());
            }

            byte[] responseBody = EntityUtils.toByteArray(response.getEntity());

            return new HTTPResponseAndroid(status, headerMap, responseBody);

        } catch(Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public void reset() {
        mClient = getHttpClient();
    }

    private DefaultHttpClient getHttpClient() {

        HttpParams params = new BasicHttpParams();
        ConnManagerParams.setMaxTotalConnections(params, 10);
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);

        // Create and initialize scheme registry
        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));

        ClientConnectionManager connMgr = new ThreadSafeClientConnManager(params, schemeRegistry);
        return new DefaultHttpClient(connMgr, params);
    }

    private class HTTPResponseAndroid extends com.example.pptcontrol.HttpResponse {

        private int status = 500;
        private Map<String, String> headers = null;
        private byte[] body = null;

        HTTPResponseAndroid(int status, Map<String, String> headers, byte[] body) {
            this.status = status;
            this.headers = headers;
            this.body = body;
        }

  
        public int getStatus() {
            return status;
        }

  
        public Map<String, String> getHeaders() {
            return headers;
        }


        public byte[] getBody() {
            return body;
        }
    }
}
